package a.client;/**
 * Author:20201002983郑雨蓓
 * Date: 2022/12/23
 * Description: 不再使用，改用Dialog对话框显示~
 */

import a.server.ConnectionProvider;
import a.server.Register;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class Login extends Application {
  private Stage stage = new Stage();//对话框窗体
  //设置数据关键字
  private String name;
  private String password;
  private Register register;

  //  public Login(Stage parentStage) {
  public void start(Stage stage) {
    Button btnRegister = new Button("注册");
    Button btnLogin = new Button("登录");
    TextField tfUsername = new TextField("bibi");
    TextField tfPassword = new TextField("20201002983");
//
//    //设置该对话框的父窗体为调用者的那个窗体
//    stage.initOwner(parentStage);
//    //设置为模态窗体，即不关闭就不能切换焦点
//    stage.initModality(Modality.WINDOW_MODAL);
//    stage.setResizable(false);
//    stage.setTitle("注册登录");
//
    HBox upBox = new HBox();
    upBox.setSpacing(10);//设置内边距
    upBox.setPadding(new Insets(10, 30, 10, 30));//设置外边距
    upBox.setAlignment(Pos.CENTER);
    upBox.getChildren().addAll(new Label("username："), tfUsername);

    HBox upBox2 = new HBox();
    upBox2.setSpacing(10);//设置内边距
    upBox2.setPadding(new Insets(10, 30, 10, 30));//设置外边距
    upBox2.setAlignment(Pos.CENTER);
    upBox2.getChildren().addAll(new Label("password："), tfPassword);

    //内容显示区域
    VBox vBox = new VBox();
    vBox.setSpacing(10);//各控件之间的间隔
    vBox.setPadding(new Insets(10, 20, 10, 20));
    vBox.getChildren().addAll(upBox, upBox2);

    HBox hBox = new HBox();
    hBox.setSpacing(10);
    hBox.setPadding(new Insets(10, 20, 10, 20));
    hBox.setAlignment(Pos.CENTER);
    hBox.getChildren().addAll(btnRegister, btnLogin);

    BorderPane mainPane = new BorderPane();
    mainPane.setCenter(vBox);
    mainPane.setBottom(hBox);

    Scene scene = new Scene(mainPane, 400, 200);
    stage.setScene(scene);
    stage.show(); //不显示对话框，由主窗体调用显示

    //**************事件响应部分***************************
    btnLogin.setOnAction(event -> {
      this.name = tfUsername.getText().trim();
      this.password = tfPassword.getText().trim();
      register = new Register(new ConnectionProvider());
      boolean result = register.login(name, password);
      if (!result) {

      }
      stage.hide();
    });

    btnRegister.setOnAction(event -> {
      this.name = tfUsername.getText().trim();
      this.password = tfPassword.getText().trim();
      Integer result = register.addUsers(name, password);
      if (result.equals(-1)) {
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle(null);
        alert.setHeaderText(null);
        alert.setContentText("用户已存在");
        alert.showAndWait();
      } else {

      }
      stage.hide();
    });
  }

  public String getGetName() {
    return this.name;
  }

  //该方法由主程序调用，阻塞式显示本对话框界面
  public void showAndWait() {
    stage.showAndWait();
  }
}
